package com.yefeng.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yefeng.usercenter.domain.entity.User;
import com.yefeng.usercenter.exception.BusinessException;
import com.yefeng.usercenter.exception.enums.ErrorCode;
import com.yefeng.usercenter.service.UserService;
import com.yefeng.usercenter.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

import static com.yefeng.usercenter.constants.UserConstants.USER_LOGIN_STATUS;

/**
* @author 叶枫
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2024-04-03 20:44:01
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    //用户加盐
    private static final String SALT = "yefeng";
    //用户登录态

    @Override
    public long userRegister(String userAccount, String userPassword, String checkPassword, String planetCode) {
        //校验
        if (StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        if (userAccount.length() < 4){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号长度小于4");
        }
        if (userPassword.length() < 8 && checkPassword.length() < 8){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码长度小于8");
        }
        if (!userPassword.equals(checkPassword)){
            throw  new BusinessException(ErrorCode.PARAMS_ERROR,"两次密码不相同");
        }
        if (planetCode.length() < 5){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"星球编号长度小于 5");
        }
        //查询用户是否重复
        long count = this.count(new LambdaQueryWrapper<User>().eq(User::getUserAccount, userAccount));
        if (count > 0){
            throw new BusinessException("返回数据错误",50003,"用户重复");
        }
        //查询用户星球编码是否重复
         count = this.count(new LambdaQueryWrapper<User>().eq(User::getPlanetCode, planetCode));
        if (count > 0){
            throw new BusinessException(ErrorCode.RESPONSE_ERROR,"星球编码重复");
        }
        //加密
        final String SALT = "yefeng";
        String encryptPassword= DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
        //插入新用户
        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserName(userAccount);
        user.setUserPassword(encryptPassword);
        user.setPlanetCode(planetCode);


        save(user);
        return 1;
    }

    @Override
    public User userLogin(String userAccount, String userPassword, HttpServletRequest request) {
        //校验
        if (StringUtils.isAnyBlank(userAccount,userPassword)){
            return null;
        }
        if (userAccount.length() < 4){
            return null;
        }
        if (userPassword.length() < 4){
            return null;
        }
        //加密
        String encryptPassword= DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
        //查询用户是否存在
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_account",userAccount);
        queryWrapper.eq("user_password",encryptPassword);
        User user = this.getOne(queryWrapper);
        if (user == null){
            return  null;
        }
        User safetyUser = isSafetyUser(user);
        //session存入用户登录态
        request.getSession().setAttribute(USER_LOGIN_STATUS,safetyUser);
        return safetyUser;


    }
    @Override
    public User isSafetyUser(User user){
        //用户数据脱敏
        User safetyUser = new User();
        safetyUser.setId(user.getId());
        safetyUser.setUserAccount(user.getUserAccount());
        safetyUser.setUserName(user.getUserName());
        safetyUser.setGender(user.getGender());
        safetyUser.setPhone(user.getPhone());
        safetyUser.setEmail(user.getEmail());
        safetyUser.setAvatarUrl(user.getAvatarUrl());
        safetyUser.setCreateTime(user.getCreateTime());
        safetyUser.setUserStatus(user.getUserStatus());
        safetyUser.setUserRole(user.getUserRole());
        safetyUser.setPlanetCode(user.getPlanetCode());
        return safetyUser;
    }


}




